package modes

import (
	"fmt"
)

/*
 * 描述: 题库选项表
 *
 *
 *****************************/

type TopicText struct {
	Id    int64  `json:"id" xorm:"id"`         //内容ID
	TId   int64  `json:"t_id" xorm:"tid"`      //题ID
	TQues string `json:"t_ques" xorm:"ques"`   //题选项
	Selec string `json:"t_selec" xorm:"selec"` //答案选项
	// 前端需要
	IsSele int `json:"is_sele" xorm:"-"` //此选项是否选中 默认0 选中：1
}

func (this *TopicText) TableName() string {
	return "topictext"
}

func (this *TopicText) Save() (int64, error) {
	return Db(0).Insert(this)
}

func (this *TopicText) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *TopicText) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *TopicText) WhereDel(tid int64) (int64, error) {
	where := fmt.Sprintf("tid = %d", tid)
	return Db(0).Where(where).Delete(this)
}

func (this *TopicText) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TopicText) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *TopicText) PList(val *Pager) error {
	//w := fmt.Sprintf( "start = %d AND phone like '%d%%' ",this.Uid,this.Phone )
	w := fmt.Sprintf("tid = %d ", this.TId)
	val.Total, _ = this.Count(w)
	var field string
	//switch ( val.Collatoin ){
	//	case 0: field = "at"        	// 领取时间
	//	case 1: field = "call_at"       // 最近一次的拨打时间
	//	case 2: field = "call_count"	// 拨打次数
	//	case 3: field = "weight"        // 权重
	//}
	list, err := this.where(val.Rules, val.Count, val.Page, w, field)
	if err == nil {
		val.List = list
	}
	return err
}
func (this *TopicText) Count(where string) (int64, error) {
	var val TopicText
	return Db(0).Where(where).Count(&val)
}
func (this *TopicText) where(fage, count, page int, where, field string) ([]TopicText, error) {
	list := make([]TopicText, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
